#include "batteryinfo.h"
#include <QFile>
#include <QProcess>
#include <QDebug>

BatteryInfo::BatteryInfo(QObject *parent) : QObject(parent)
{

}


QString catFileContentProcess(const QString& path)
{
    QProcess pro;
    QString cmd = QString("cat %1").arg(path);
    pro.start(cmd);
    pro.waitForFinished();
    QString output = pro.readLine();
    qDebug() << "catFileContentProcess " << path << output;
    return output;
}

void BatteryInfo::queryBatteryInfo(Battery_State &state, quint32 &percent)
{
    state = BatteryInfo::Battery_Invalid;
    percent = 100;

    QFile acOnline("/sys/class/power_supply/AC/online");
    QFile adpOnline("/sys/class/power_supply/ADP0/online");

    QFile batteryCap0("/sys/class/power_supply/BAT0/capacity");
    QFile batteryStatus0("/sys/class/power_supply/BAT0/status");

    QFile batteryCap1("/sys/class/power_supply/BAT1/capacity");
    QFile batteryStatus1("/sys/class/power_supply/BAT1/status");

    QFile battery_tmp_cap("/tmp/power_capacity");

    if (batteryCap1.exists() && batteryStatus1.exists())
    {
        if (battery_tmp_cap.exists() && battery_tmp_cap.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            percent = QString(battery_tmp_cap.readAll()).simplified().toUInt();
            battery_tmp_cap.close();
        }
        else
        {
            batteryCap1.open(QIODevice::ReadOnly | QIODevice::Text);
            percent = QString(batteryCap1.readAll()).simplified().toUInt();
            batteryCap1.close();
        }

        batteryStatus1.open(QIODevice::ReadOnly | QIODevice::Text);
        QString batteryStatusString = QString(batteryStatus1.readAll()).simplified();
        state = batteryStatusString.contains("Discharging", Qt::CaseInsensitive) ? \
                    BatteryInfo::Battery_Discharging : \
                    BatteryInfo::Battery_Charging;
        batteryStatus1.close();
//        QString batteryStatusString = catFileContentProcess("/sys/class/power_supply/BAT1/status").simplified();
//        state = batteryStatusString.contains("Discharging", Qt::CaseInsensitive) ? \
//                    BatteryInfo::Battery_Discharging : \
//                    BatteryInfo::Battery_Charging;
        return;
    }

    if (batteryCap0.exists() && batteryStatus0.exists())
    {
        if (battery_tmp_cap.exists() && battery_tmp_cap.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            percent = QString(battery_tmp_cap.readAll()).simplified().toUInt();
            battery_tmp_cap.close();
        }
        else
        {
            batteryCap0.open(QIODevice::ReadOnly | QIODevice::Text);
            percent = QString(batteryCap0.readAll()).simplified().toUInt();
            batteryCap0.close();
        }

        batteryStatus0.open(QIODevice::ReadOnly | QIODevice::Text);
        QString batteryStatusString = QString(batteryStatus0.readAll()).simplified();
        state = batteryStatusString.contains("Discharging", Qt::CaseInsensitive) ? \
                    BatteryInfo::Battery_Discharging : \
                    BatteryInfo::Battery_Charging;
        batteryStatus0.close();

//        QString batteryStatusString = catFileContentProcess("/sys/class/power_supply/BAT0/status").simplified();
//        state = batteryStatusString.contains("Discharging", Qt::CaseInsensitive) ? \
//                    BatteryInfo::Battery_Discharging : \
//                    BatteryInfo::Battery_Charging;
        return;
    }

    // check nobattery with ac/adp
    if (acOnline.exists())
    {
        acOnline.open(QIODevice::ReadOnly | QIODevice::Text);
        if (QString(acOnline.readAll()).simplified().toInt())
            state = BatteryInfo::Battery_Nobattery;
        acOnline.close();
    }
    else if (adpOnline.exists())
    {
        adpOnline.open(QIODevice::ReadOnly | QIODevice::Text);
        if (QString(adpOnline.readAll()).simplified().toInt())
            state = BatteryInfo::Battery_Nobattery;
        adpOnline.close();
    }

}
